/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package runebound.ui.ciudad;

import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import runebound.manejadores.RBControladorJuego;
import runebound.manejadores.eventos.RBEventoNuevoTurno;
import runebound.modelo.Ciudad;
import runebound.modelo.heores.Heroe;
import runebound.modelo.heores.items.Item;

/**
 *
 * @author Miguel (alakat@gmail.com)
 */
public class RBCiudadDialog extends javax.swing.JDialog implements ListSelectionListener{

    private Ciudad ciudad;
    
    /**
     * Creates new form RBCiudadDialog
     */
    public RBCiudadDialog(java.awt.Frame parent, boolean modal,Ciudad ciudad) {
        super(parent, modal);
        initComponents();
        this.ciudad=ciudad;
        this.updateInterfaz();
        items.addListSelectionListener(this);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        titulo = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        items = new javax.swing.JList();
        robarNuevoItem = new javax.swing.JToggleButton();
        itemSeleccionadoTitulo = new javax.swing.JLabel();
        itemSeleccionadoEfecto = new javax.swing.JLabel();
        itemSeleccionadoTipo = new javax.swing.JLabel();
        itemSeleccionadoModoEmpleo = new javax.swing.JLabel();
        comparaItem = new javax.swing.JButton();
        itemSeleccionadoORO = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        curarOro = new javax.swing.JLabel();
        curarBurron = new javax.swing.JButton();
        reponerFatigaBurron = new javax.swing.JButton();
        fatigaOro = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        exitButton = new javax.swing.JButton();
        informacionLabel = new javax.swing.JLabel();

        jButton3.setText("jButton3");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        titulo.setFont(new java.awt.Font("Lucida Grande", 2, 24)); // NOI18N
        titulo.setForeground(new java.awt.Color(153, 153, 153));
        titulo.setText("Ciudad:");
        getContentPane().add(titulo, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 470, -1));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), "Mercado"));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        items.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(items);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 140, 170));

        robarNuevoItem.setText("Robar items");
        robarNuevoItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                robarNuevoItemActionPerformed(evt);
            }
        });
        jPanel1.add(robarNuevoItem, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 140, -1));

        itemSeleccionadoTitulo.setText("titulo");
        jPanel1.add(itemSeleccionadoTitulo, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 60, 110, -1));

        itemSeleccionadoEfecto.setText("Efecto");
        jPanel1.add(itemSeleccionadoEfecto, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 120, 120, 70));

        itemSeleccionadoTipo.setText("tipo");
        jPanel1.add(itemSeleccionadoTipo, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 80, 120, 20));

        itemSeleccionadoModoEmpleo.setText("modo");
        jPanel1.add(itemSeleccionadoModoEmpleo, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 100, 120, 20));

        comparaItem.setText("Comprar");
        comparaItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comparaItemActionPerformed(evt);
            }
        });
        jPanel1.add(comparaItem, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 190, 160, -1));

        itemSeleccionadoORO.setBackground(new java.awt.Color(0, 0, 0));
        itemSeleccionadoORO.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        itemSeleccionadoORO.setForeground(new java.awt.Color(255, 255, 0));
        itemSeleccionadoORO.setText("12");
        itemSeleccionadoORO.setOpaque(true);
        jPanel1.add(itemSeleccionadoORO, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 60, 20, 20));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 320, 240));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), "Templo"));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setText("Curar heridas");
        jPanel2.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, -1, -1));

        curarOro.setBackground(new java.awt.Color(0, 0, 0));
        curarOro.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        curarOro.setForeground(new java.awt.Color(255, 255, 0));
        curarOro.setText("1/h");
        curarOro.setOpaque(true);
        jPanel2.add(curarOro, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 30, 30, 20));

        curarBurron.setText("Curar heridas");
        curarBurron.setToolTipText("1 Oro por herida");
        curarBurron.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                curarBurronActionPerformed(evt);
            }
        });
        jPanel2.add(curarBurron, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 120, -1));

        reponerFatigaBurron.setFont(new java.awt.Font("Lucida Grande", 0, 10)); // NOI18N
        reponerFatigaBurron.setText("Reponer la fatiga");
        reponerFatigaBurron.setToolTipText("1 Oro repone toda la fatiga");
        reponerFatigaBurron.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reponerFatigaBurronActionPerformed(evt);
            }
        });
        jPanel2.add(reponerFatigaBurron, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 150, 120, -1));

        fatigaOro.setBackground(new java.awt.Color(0, 0, 0));
        fatigaOro.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        fatigaOro.setForeground(new java.awt.Color(255, 255, 0));
        fatigaOro.setText("1");
        fatigaOro.setOpaque(true);
        jPanel2.add(fatigaOro, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 120, 20, 20));

        jLabel3.setText("Reponer Fatiga");
        jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, -1, -1));

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 40, 140, 240));

        exitButton.setText("Salir de la ciudad");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });
        getContentPane().add(exitButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 280, -1, -1));

        informacionLabel.setText("Vista de la ciudad");
        getContentPane().add(informacionLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 320, 460, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void robarNuevoItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_robarNuevoItemActionPerformed
        // TODO add your handling code here:
        RBControladorJuego.getInstance().getControladorAventura().nuevoItemMercado(ciudad);
        this.updateInterfaz();
    }//GEN-LAST:event_robarNuevoItemActionPerformed

    private void curarBurronActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_curarBurronActionPerformed
        // TODO add your handling code here:
        Heroe h = RBControladorJuego.getInstance().getHeroeActivo();
        if(h.getOro()<1){
            this.noMásDinero(1);
        }else{
            h.gastarOro(1);
            h.curar(1);
            RBControladorJuego.getInstance().getWindow().repaint();
            informacionLabel.setText("Se cura una HERIDA por 1 ORO");
        }
    }//GEN-LAST:event_curarBurronActionPerformed

    private void reponerFatigaBurronActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reponerFatigaBurronActionPerformed
        // TODO add your handling code here:
        Heroe h = RBControladorJuego.getInstance().getHeroeActivo();
        if(h.getOro()<1){
            this.noMásDinero(1);
        }else{
            h.gastarOro(1);
            h.recuperar(h.getFatiga());
            RBControladorJuego.getInstance().getWindow().repaint();
            informacionLabel.setText("Se repone completamente la fatiga por 1 de ORO");
        }
    }//GEN-LAST:event_reponerFatigaBurronActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        RBControladorJuego.getInstance().recibirEvento(
                                            new RBEventoNuevoTurno());
    }//GEN-LAST:event_exitButtonActionPerformed

    private void comparaItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comparaItemActionPerformed
        Heroe h = RBControladorJuego.getInstance().getHeroeActivo();
        Item item = (Item)items.getSelectedValue();
        if(h.getOro()<item.getOro()){
            this.noMásDinero(item.getOro());
        }else{
            h.gastarOro(item.getOro());
            //sale de la lista de items
            RBControladorJuego.getInstance().getControladorAventura().mercado(ciudad).remove(item);
            h.addItem(item);
            this.repaint();
            RBControladorJuego.getInstance().getWindow().repaint();
            informacionLabel.setText(item.getTitulo()+" ha sido comprado");
        }
    }//GEN-LAST:event_comparaItemActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton comparaItem;
    private javax.swing.JButton curarBurron;
    private javax.swing.JLabel curarOro;
    private javax.swing.JButton exitButton;
    private javax.swing.JLabel fatigaOro;
    private javax.swing.JLabel informacionLabel;
    private javax.swing.JLabel itemSeleccionadoEfecto;
    private javax.swing.JLabel itemSeleccionadoModoEmpleo;
    private javax.swing.JLabel itemSeleccionadoORO;
    private javax.swing.JLabel itemSeleccionadoTipo;
    private javax.swing.JLabel itemSeleccionadoTitulo;
    private javax.swing.JList items;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton reponerFatigaBurron;
    private javax.swing.JToggleButton robarNuevoItem;
    private javax.swing.JLabel titulo;
    // End of variables declaration//GEN-END:variables

    /**
     * Actualiza la intefaz de usuario
     */
    private void updateInterfaz() {
        this.titulo.setText(ciudad.getTitulo());
        this.items.setModel(new MercadoListModel(ciudad,
                RBControladorJuego.getInstance().getControladorAventura().mercado(ciudad)));
        this.repaint();
    }
    
    
    
    /**
     * Dialogo para indicar que no hay oro suficiente
     * @param oro 
     */
    private void noMásDinero(int oro){
        JOptionPane.showMessageDialog(this, "No tienes directo, el coste es de "+oro);
        informacionLabel.setText("No hay dinero suficiente");
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        int index  = e.getFirstIndex();
        Item item = (Item)items.getModel().getElementAt(index);
        itemSeleccionadoTitulo.setText(item.getTitulo());
        itemSeleccionadoModoEmpleo.setText(item.getModoEmpleo().toString());
        itemSeleccionadoTipo.setText(item.getTipo().toString());
        itemSeleccionadoORO.setText(""+item.getOro());
        itemSeleccionadoEfecto.setText(item.describirEfecto());
                
    }
}
